package com.bcbim.api_web.role.controller;

import com.bcbim.authentication_interface.ability.dao.RoleAbilityDAO;
import com.bcbim.authentication_interface.role.dao.RoleMenuDAO;
import com.bcbim.authentication_interface.role.dto.RoleMenuAbilityDTO;
import com.bcbim.authentication_interface.role.dto.SaveRoleAuthenticationDTO;
import com.bcbim.authentication_interface.role.service.RoleAuthenticationService;
import com.bcbim.authentication_interface.role.vo.MenuAbilityVO;
import com.bcbim.common.response.vo.ReturnMsg;
import com.bcbim.common.sys.Code;
import com.bcbim.common.sys.ParamException;
import com.bcbim.common.util.GuidUtil;
import com.bcbim.common.util.ParamCheck;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Description: 角色权限
 * @Author: ye
 * @Date: 2021/7/6
 * @Version: V1.0
 **/
@RestController
@RequestMapping("/roleAuthentication")
public class RoleAuthenticationController {

    @Reference(protocol = "dubbo", group = "roleAuthentication", version = "1.0.0")
    private RoleAuthenticationService roleAuthenticationServiceImpl;

    /**
     * @Description: 获取菜单和功能列表
     * @Param: RoleMenuAbilityQuery
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/7/7
     */
    @PostMapping("listMenuAndAbility")
    public ReturnMsg listMenuAndAbility(RoleMenuAbilityDTO query) {
        try {
            ParamCheck.idCheck(query.getRoleId());
            ParamCheck.stringNotEmpty(query.getApplicationPlatform(), 1);
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        List<MenuAbilityVO> arrayMenu = roleAuthenticationServiceImpl.getAllMenuByApplication(query.getApplicationPlatform());
        List<MenuAbilityVO> arrayAbility = roleAuthenticationServiceImpl.getAllAbilityByApplication(query.getApplicationPlatform());
        List<String> roleMenu = roleAuthenticationServiceImpl.getRoleMenuAuthentication(query);
        List<String> roleAbility = roleAuthenticationServiceImpl.getRoleAbilityAuthentication(query);

        //功能map
        Map<String, List<MenuAbilityVO>> abilityMap = new HashMap<>();
        for (MenuAbilityVO a : arrayAbility) {
            a.setCheck(roleAbility.contains(a.getId()));

            if (abilityMap.containsKey(a.getParentId())) {
                abilityMap.get(a.getParentId()).add(a);
            } else {
                List<MenuAbilityVO> newArray = new ArrayList<>();
                newArray.add(a);
                abilityMap.put(a.getParentId(), newArray);
            }
        }

        List<MenuAbilityVO> resultTree = new ArrayList<>();
        for (MenuAbilityVO m : arrayMenu) {
            m.setCheck(roleMenu.contains(m.getId()));

            if (StringUtils.isEmpty(m.getParentId())) {
                m.setChildren(getChildren(m.getId(), arrayMenu, abilityMap, roleMenu));
                resultTree.add(m);
            }
        }
        return ReturnMsg.consSuccess(resultTree);
    }

    /**
     * @Description: 获取菜单子节点
     * @Param: 节点id
     * @Param: List<MenuAbilityVO> 菜单
     * @Param: Map<String, List<MenuAbilityVO>> 功能map
     * @Param: List<String> 拥有菜单id
     * @Return: List<MenuAbilityVO>
     * @Author: ye
     * @Date: 2021/7/7
     */
    private List<MenuAbilityVO> getChildren(String id, List<MenuAbilityVO> arrayMenu,
                                            Map<String, List<MenuAbilityVO>> abilityMap, List<String> roleMenu) {
        List<MenuAbilityVO> children = new ArrayList<>();
        for (MenuAbilityVO m : arrayMenu) {
            m.setCheck(roleMenu.contains(m.getId()));

            if (StringUtils.isNotEmpty(m.getParentId()) && m.getParentId().equals(id)) {
                m.setChildren(getChildren(m.getId(), arrayMenu, abilityMap, roleMenu));
                children.add(m);
            }
        }
        if (children.size() == 0) {
            children = abilityMap.get(id);
        }
        return children;
    }

    /**
     * @Description: 角色查询
     * @Param: roleName
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/7/7
     */
    @PostMapping("selectRole")
    public ReturnMsg selectRole(String roleName) {
        List<Code> list = roleAuthenticationServiceImpl.selectRole(roleName);
        return ReturnMsg.consSuccess(list);
    }

    /**
     * @Description: 保存角色菜单功能权限
     * @Param: SaveRoleAuthenticationQuery
     * @Return: ReturnMsg
     * @Author: ye
     * @Date: 2021/7/7
     */
    @PostMapping("saveRoleAuthentication")
    public ReturnMsg saveRoleAuthentication(SaveRoleAuthenticationDTO query) {
        try {
            ParamCheck.idCheck(query.getLevelTwoId());
        } catch (ParamException e) {
            return ReturnMsg.consFail(ParamException.PARAMETER_ERROR);
        }
        //清除旧数据
        List<String> childrenPage = roleAuthenticationServiceImpl.getLevelThreeMenu(query.getLevelTwoId());
        List<String> arrayAbility = roleAuthenticationServiceImpl.getAbilityByMenuId(childrenPage);
        roleAuthenticationServiceImpl.clearRoleAuthentication(query.getRoleId(), childrenPage, arrayAbility);

        //区分功能和页面
        List<String> distinguishMenu = roleAuthenticationServiceImpl.distinguishMenu(query.getCheckId());
        List<String> distinguishAbility = roleAuthenticationServiceImpl.distinguishAbility(query.getCheckId());

        List<RoleMenuDAO> roleMenuList = new ArrayList<>();
        List<RoleAbilityDAO> roleAbilityList = new ArrayList<>();
        for (String menuId : distinguishMenu) {
            RoleMenuDAO m = new RoleMenuDAO();
            m.setId(GuidUtil.get());
            m.setMenuId(menuId);
            m.setRoleId(query.getRoleId());
            roleMenuList.add(m);
        }
        for (String abilityId : distinguishAbility) {
            RoleAbilityDAO a = new RoleAbilityDAO();
            a.setId(GuidUtil.get());
            a.setAbilityId(abilityId);
            a.setRoleId(query.getRoleId());
            roleAbilityList.add(a);
        }

        roleAuthenticationServiceImpl.saveRoleAuthentication(roleMenuList, roleAbilityList);

        String rootId = roleAuthenticationServiceImpl.getMenuRootId(query.getLevelTwoId());
        roleAuthenticationServiceImpl.clearRoleAuthentication(query.getRoleId(), Collections.singletonList(rootId), new ArrayList<>());
        int count = roleAuthenticationServiceImpl.countRoleLevelTwoMenu(query.getRoleId(), rootId);
        if (count > 0) {
            RoleMenuDAO roleMenuDO = new RoleMenuDAO();
            roleMenuDO.setId(GuidUtil.get());
            roleMenuDO.setRoleId(query.getRoleId());
            roleMenuDO.setMenuId(rootId);
            roleAuthenticationServiceImpl.saveRoleAuthentication(Collections.singletonList(roleMenuDO), new ArrayList<>());
        }
        return ReturnMsg.consSuccess("保存成功");
    }
}
